package com.bawei.library_mvi.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.bawei.library_mvi.R

abstract class BaseActivity<VDB:ViewDataBinding> : AppCompatActivity() {
    protected lateinit var viewDataBinding:VDB
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewDataBinding = DataBindingUtil.setContentView(this,getLayout())
        initSatusBar()
        initEnv()
        initView()
        initData()
        initEvent()
    }



    //状态栏
    private fun initSatusBar() {

    }
    open fun initEnv(){} //初始化环境：viewmodel
    abstract fun getLayout():Int //返回布局id
    open fun initView(){} //初始化view
    open fun initData(){}//初始化数据
    open fun initEvent(){}//初始化 事件监听

    //返回状态栏的高度
    protected fun geStatusBartHeight():Int{
        var resourceId= resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0
    }

    //吐司方法
    fun showMsg(msg:String){
        //判断是否为主线程
        if(Looper.getMainLooper().thread == Thread.currentThread()){
            Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
        }else{
            //子线程切换到主线程
            handler.post{
                Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
            }
        }
    }
    private val  handler:Handler = Handler(Looper.getMainLooper())
    //解决handler的内存泄漏问题
    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }
}